package com.advert.provider.security;

import com.advert.model.security.*;
import com.advert.provider.ProviderBase;
import com.advert.util.Event;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Events;

import javax.persistence.NoResultException;
import java.util.List;

@Name("SecurityProvider")
@Scope(ScopeType.EVENT)
@AutoCreate
public class SecurityProvider extends ProviderBase {

    public UserEntity getUserByLogin(String login) {
        UserEntity retVal = null;
        try {
            retVal = (UserEntity)entityManager.
                    createNamedQuery("UserEntity.getByLogin").
                    setParameter("login", login).
                    getSingleResult();
        } catch (NoResultException e) {}
        return retVal;
    }

    public UserEntity getUserByLoginAndMail(String login, String mail) {
        UserEntity retVal = null;
        try {
            retVal = (UserEntity)entityManager.
                    createNamedQuery("UserEntity.getByLoginAndMail").
                    setParameter("login", login).
                    setParameter("email", mail).
                    getSingleResult();
        } catch (NoResultException e) {}
        return retVal;
    }

    public UserEntity createUser(UserEntity user) {
        entityManager.persist(user);
        return user;
    }

    public UserEntity updateUser(UserEntity user) {
        return entityManager.merge(user);   
    }

    public UserEntity getUserById(Integer id) {
        UserEntity retVal = null;
        try {
            retVal = (UserEntity)entityManager.
                    createNamedQuery("UserEntity.getById").
                    setParameter("id", id).
                    getSingleResult();
        } catch (NoResultException e) {}
        return retVal;
    }

    @SuppressWarnings("unchecked")
    public List<SecurityEntity> getAllSecurity() {
        return entityManager.
                createNamedQuery("SecurityEntity.getAll").
                getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<UserEntity> getUsersByRole(Role role) {
        return entityManager.
                createNamedQuery("UserEntity.getByRole").
                setParameter("role", role).
                getResultList();
    }

    public UserInfoEntity getUserInfo(Integer userId) {
        UserInfoEntity retVal = (UserInfoEntity)entityManager.
                                createNamedQuery("UserInfoEntity.getByUserId").
                                setParameter("userId", userId).
                                getSingleResult();

        Events.instance().raiseEvent(Event.ENTITY_ACCESS, retVal, AccessMode.VIEW);
        return retVal;
    }

    public UserInfoEntity updateUserInfo(UserInfoEntity info) {
        Events.instance().raiseEvent(Event.ENTITY_ACCESS, info, AccessMode.EDIT);

        return entityManager.merge(info);
    }
}
